package coroutines.coroutine.cancel

import kotlinx.coroutines.*
import java.lang.Exception

class CancelFinallyTest {
}

fun main() {
    runBlocking {
        val job = launch {
            try {
                repeat(100) {
                    println("job: I'm sleeping $it ...")
                    delay(500L)
                }
            } catch (e: Throwable) {
//                e.printStackTrace()
            } finally {
                try {
                    println("job: I'm running finally start0")

                    withContext(NonCancellable) {
                        delay(100)
                    }

                    println("job: I'm running finally start1")
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }

        delay(1300L)
        println("main: I'm tried waiting!")
        job.cancelAndJoin()
        println("main: Now I can quit.")
    }
}